﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Enterprise.DataFactory;
using Enterprise.Model.Jihuabu;
using System.Data;
namespace Enterprise.Data.Jihuabu
{
    /// <summary>
    /// 计划经营部项目表数据操作
    /// </summary>
    public class M01_XMBData
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public M01_XMBData()
		{}
		#region  Method


		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(string AFEBH)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM JHB_M01_XMB");
			strSql.Append(" where AFEBH='"+AFEBH+"' ");
			return DbExecute.Exist(strSql.ToString());
		}

		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(M01_XMBEntity model)
		{
			StringBuilder strSql=new StringBuilder();
			StringBuilder strSql1=new StringBuilder();
			StringBuilder strSql2=new StringBuilder();
			if (model.AFEBH != null)
			{
				strSql1.Append("AFEBH,");
				strSql2.Append("'"+model.AFEBH+"',");
			}
			if (model.XMSSGS != null)
			{
				strSql1.Append("XMSSGS,");
				strSql2.Append("'"+model.XMSSGS+"',");
			}
			if (model.XMFL != null)
			{
				strSql1.Append("XMFL,");
				strSql2.Append("'"+model.XMFL+"',");
			}
			if (model.XMMC != null)
			{
				strSql1.Append("XMMC,");
				strSql2.Append("'"+model.XMMC+"',");
			}
			if (model.AFEPZ_MY != null)
			{
				strSql1.Append("AFEPZ_MY,");
				strSql2.Append(""+model.AFEPZ_MY+",");
			}
			if (model.AFEPZ_JG != null)
			{
				strSql1.Append("AFEPZ_JG,");
				strSql2.Append(""+model.AFEPZ_JG+",");
			}
			if (model.SHIJI_JG != null)
			{
				strSql1.Append("SHIJI_JG,");
				strSql2.Append(""+model.SHIJI_JG+",");
			}
			if (model.ZT != null)
			{
				strSql1.Append("ZT,");
				strSql2.Append("'"+model.ZT+"',");
			}
			if (model.LRR != null)
			{
				strSql1.Append("LRR,");
				strSql2.Append("'"+model.LRR+"',");
			}
			if (model.KGSJ != null)
			{
				strSql1.Append("KGSJ,");
				strSql2.Append("'"+model.KGSJ+"',");
			}
			if (model.WGSJ != null)
			{
				strSql1.Append("WGSJ,");
				strSql2.Append("'"+model.WGSJ+"',");
			}
			strSql.Append("insert into JHB_M01_XMB(");
			strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
			strSql.Append(")");
			strSql.Append(" values (");
			strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
			strSql.Append(")");
			int rows=DbExecute.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(M01_XMBEntity model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update JHB_M01_XMB set ");
			if (model.XMSSGS != null)
			{
				strSql.Append("XMSSGS='"+model.XMSSGS+"',");
			}
			else
			{
				strSql.Append("XMSSGS= null ,");
			}
			if (model.XMFL != null)
			{
				strSql.Append("XMFL='"+model.XMFL+"',");
			}
			else
			{
				strSql.Append("XMFL= null ,");
			}
			if (model.XMMC != null)
			{
				strSql.Append("XMMC='"+model.XMMC+"',");
			}
			else
			{
				strSql.Append("XMMC= null ,");
			}
			if (model.AFEPZ_MY != null)
			{
				strSql.Append("AFEPZ_MY="+model.AFEPZ_MY+",");
			}
			else
			{
				strSql.Append("AFEPZ_MY= null ,");
			}
			if (model.AFEPZ_JG != null)
			{
				strSql.Append("AFEPZ_JG="+model.AFEPZ_JG+",");
			}
			else
			{
				strSql.Append("AFEPZ_JG= null ,");
			}
			if (model.SHIJI_JG != null)
			{
				strSql.Append("SHIJI_JG="+model.SHIJI_JG+",");
			}
			else
			{
				strSql.Append("SHIJI_JG= null ,");
			}
			if (model.ZT != null)
			{
				strSql.Append("ZT='"+model.ZT+"',");
			}
			else
			{
				strSql.Append("ZT= null ,");
			}
			if (model.LRR != null)
			{
				strSql.Append("LRR='"+model.LRR+"',");
			}
			else
			{
				strSql.Append("LRR= null ,");
			}
			if (model.KGSJ != null)
			{
				strSql.Append("KGSJ='"+model.KGSJ+"',");
			}
			else
			{
				strSql.Append("KGSJ= null ,");
			}
			if (model.WGSJ != null)
			{
				strSql.Append("WGSJ='"+model.WGSJ+"',");
			}
			else
			{
				strSql.Append("WGSJ= null ,");
			}
			int n = strSql.ToString().LastIndexOf(",");
			strSql.Remove(n, 1);
			strSql.Append(" where AFEBH='"+ model.AFEBH+"' ");
			int rowsAffected=DbExecute.ExecuteSql(strSql.ToString());
			if (rowsAffected > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(string AFEBH)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete FROM JHB_M01_XMB ");
			strSql.Append(" where AFEBH='"+AFEBH+"' " );
			int rowsAffected=DbExecute.ExecuteSql(strSql.ToString());
			if (rowsAffected > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string AFEBHlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete FROM JHB_M01_XMB ");
			strSql.Append(" where AFEBH in ("+AFEBHlist + ")  ");
			int rows=DbExecute.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public M01_XMBEntity GetModel(string AFEBH)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  ");
			strSql.Append(" AFEBH,XMSSGS,XMFL,XMMC,AFEPZ_MY,AFEPZ_JG,SHIJI_JG,ZT,LRR,KGSJ,WGSJ ");
			strSql.Append(" FROM JHB_M01_XMB ");
			strSql.Append(" where AFEBH='"+AFEBH+"' " );
			M01_XMBEntity model=new M01_XMBEntity();
			DataSet ds=DbExecute.GetDataSet(strSql.ToString());
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public M01_XMBEntity DataRowToModel(DataRow row)
		{
			M01_XMBEntity model=new M01_XMBEntity();
			if (row != null)
			{
				if(row["AFEBH"]!=null)
				{
					model.AFEBH=row["AFEBH"].ToString();
				}
				if(row["XMSSGS"]!=null)
				{
					model.XMSSGS=row["XMSSGS"].ToString();
				}
				if(row["XMFL"]!=null)
				{
					model.XMFL=row["XMFL"].ToString();
				}
				if(row["XMMC"]!=null)
				{
					model.XMMC=row["XMMC"].ToString();
				}
				if(row["AFEPZ_MY"]!=null && row["AFEPZ_MY"].ToString()!="")
				{
					model.AFEPZ_MY=decimal.Parse(row["AFEPZ_MY"].ToString());
				}
				if(row["AFEPZ_JG"]!=null && row["AFEPZ_JG"].ToString()!="")
				{
					model.AFEPZ_JG=decimal.Parse(row["AFEPZ_JG"].ToString());
				}
				if(row["SHIJI_JG"]!=null && row["SHIJI_JG"].ToString()!="")
				{
					model.SHIJI_JG=decimal.Parse(row["SHIJI_JG"].ToString());
				}
				if(row["ZT"]!=null)
				{
					model.ZT=row["ZT"].ToString();
				}
				if(row["LRR"]!=null)
				{
					model.LRR=row["LRR"].ToString();
				}
				if(row["KGSJ"]!=null)
				{
					model.KGSJ=row["KGSJ"].ToString();
				}
				if(row["WGSJ"]!=null)
				{
					model.WGSJ=row["WGSJ"].ToString();
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select AFEBH,XMSSGS,XMFL,XMMC,AFEPZ_MY,AFEPZ_JG,SHIJI_JG,ZT,LRR,KGSJ,WGSJ ");
			strSql.Append(" FROM JHB_M01_XMB ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
            strSql.Append(" order by kgsj desc");
			return DbExecute.GetDataSet(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM JHB_M01_XMB ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
            object obj = DbExecute.ExecuteScalar(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.AFEBH desc");
			}
			strSql.Append(")AS R, T.*  FROM JHB_M01_XMB T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.R between {0} and {1}", startIndex, endIndex);
			return DbExecute.GetDataSet(strSql.ToString());
		}

		/*
		*/

		#endregion  Method
		#region  MethodEx		

        /// <summary>
        /// 变更数据同时也变更编号
        /// </summary>
        /// <param name="model"></param>
        /// <param name="new_afebh"></param>
        /// <returns></returns>
        public bool Change(M01_XMBEntity model, string new_afebh)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update JHB_M01_XMB set ");
            strSql.Append("AFEBH= '"+new_afebh+"' ,");
            if (model.XMSSGS != null)
            {
                strSql.Append("XMSSGS='" + model.XMSSGS + "',");
            }
            else
            {
                strSql.Append("XMSSGS= null ,");
            }
            if (model.XMFL != null)
            {
                strSql.Append("XMFL='" + model.XMFL + "',");
            }
            else
            {
                strSql.Append("XMFL= null ,");
            }
            if (model.XMMC != null)
            {
                strSql.Append("XMMC='" + model.XMMC + "',");
            }
            else
            {
                strSql.Append("XMMC= null ,");
            }
            if (model.AFEPZ_MY != null)
            {
                strSql.Append("AFEPZ_MY=" + model.AFEPZ_MY + ",");
            }
            else
            {
                strSql.Append("AFEPZ_MY= null ,");
            }
            if (model.AFEPZ_JG != null)
            {
                strSql.Append("AFEPZ_JG=" + model.AFEPZ_JG + ",");
            }
            else
            {
                strSql.Append("AFEPZ_JG= null ,");
            }
            if (model.SHIJI_JG != null)
            {
                strSql.Append("SHIJI_JG=" + model.SHIJI_JG + ",");
            }
            else
            {
                strSql.Append("SHIJI_JG= null ,");
            }
            if (model.ZT != null)
            {
                strSql.Append("ZT='" + model.ZT + "',");
            }
            else
            {
                strSql.Append("ZT= null ,");
            }
            if (model.LRR != null)
            {
                strSql.Append("LRR='" + model.LRR + "',");
            }
            else
            {
                strSql.Append("LRR= null ,");
            }
            if (model.KGSJ != null)
            {
                strSql.Append("KGSJ='" + model.KGSJ + "',");
            }
            else
            {
                strSql.Append("KGSJ= null ,");
            }
            if (model.WGSJ != null)
            {
                strSql.Append("WGSJ='" + model.WGSJ + "',");
            }
            else
            {
                strSql.Append("WGSJ= null ,");
            }
            int n = strSql.ToString().LastIndexOf(",");
            strSql.Remove(n, 1);
            strSql.Append(" where AFEBH='" + model.AFEBH + "' ");
            int rowsAffected = DbExecute.ExecuteSql(strSql.ToString());
            if (rowsAffected > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        #endregion  MethodEx
    }
}
